package com.hzhc.controller.baseinfo;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CntrLogService;
import com.hzhc.utils.EasypoiExcelUtils;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.CntrLogVo;
import com.hzhc.vo.ProductionVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author dongcy
 * @date 2024/5/29 14:02
 * @description CntrLogController
 */
@RestController
@RequestMapping("/cntrLog")
@RequiredArgsConstructor
public class CntrLogController extends BaseController {

    private final CntrLogService logService;

    /**
     * 统计查询--生产日报表
     * @param vo
     * @return
     */
    @RequestMapping("/getProductionList")
    @ResponseBody
    public ResultUtil<List<ProductionVo>> getProductionList(@RequestBody ProductionVo vo) {
        List<ProductionVo> list = logService.getProductionList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 生产日报表--导出(20240613暂不用)
     * @param year
     * @param response
     */
    @ResponseBody
    @RequestMapping("/deriveProduction")
    @SysTemLog(operModul = "统计查询-生产日报表导出", operType = "导出", operDesc = "生产日报表导出功能", modular = "PC")
    public void deriveProduction(String year, HttpServletResponse response) {
        TemplateExportParams params = new TemplateExportParams(
                "static/download/生产日报表.xlsx");
        Map<String, Object> productionList = logService.getProductionList1(new ProductionVo().setYear(year));

        String title = "";
        if(year.length() == 4){
            title = year + "年度生产报表";
        }else if(year.length() == 7 ){
            title = year + "月度生产报表";
        }else{
            title = year + "日生产报表";
        }
        productionList.put("title",title);
        try {
            Workbook workbook = ExcelExportUtil.exportExcel(params, productionList);
            EasypoiExcelUtils.export(response,workbook,title);
        } catch (Exception e) {
            System.out.println("导出模板Excel，失败:" + e);
        }
    }

    /**
     * 场地理货量统计list 列表
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getSiteTallyList")
    public ResultUtil<List<CntrLogVo>> getSiteTallyList(HttpServletRequest request,@RequestBody CntrLogVo vo){
        Page<CntrLogVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<CntrLogVo> list = logService.getSiteTallyList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 场地理货量统计-折线图
     * @param vo
     * @return
     */
    @RequestMapping("/getSiteTallyLine")
    public ResultUtil<Map<String, Object>> statisticsData(@RequestBody CntrLogVo vo) {
        return ResultUtil.build(logService.getSiteTallyLine(vo),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 箱动态查询
     * @param vo
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public ResultUtil<List<CntrLogVo>> getList(@RequestBody CntrLogVo vo) {
        Page<CntrLogVo> pagination = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<CntrLogVo> cntrLogVos = logService.getList(vo, pagination);
        return ResultUtil.build(cntrLogVos, pagination.getTotal(),pagination.getPages(), ResultUtilCodeEnum.SUCCESS);
    }
}
